﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HanGu.Services.Interface;
using HanGu.Common;
using HanGu.Entity;

namespace Hangu.WebClient.Controllers
{

    public class BaseController : Controller
    {
        protected static readonly IUniversallyContent serviceUniversally = ServiceFactory.GetService<IUniversallyContent>();
        protected static readonly IPhilosopherBema servicePhilosopherBema = ServiceFactory.GetService<IPhilosopherBema>();
        protected static readonly INews serviceNews = ServiceFactory.GetService<INews>();
        protected static readonly IEveryDayWisdom serviceEveryDayWisdom = ServiceFactory.GetService<IEveryDayWisdom>();
        protected static readonly IModule serviceModule = ServiceFactory.GetService<IModule>();
        protected static readonly ITravelStudy serviceTravel = ServiceFactory.GetService<ITravelStudy>();
        protected static readonly ICultureEducation serviceEducation = ServiceFactory.GetService<ICultureEducation>();
        protected static readonly IActivePlan servicePlan = ServiceFactory.GetService<IActivePlan>();
        protected static readonly ILibrary serviceLibrary = ServiceFactory.GetService<ILibrary>();
        protected static readonly IProduct serviceProduct = ServiceFactory.GetService<IProduct>();
        protected static readonly IDictionary serviceDict = ServiceFactory.GetService<IDictionary>();
        protected static readonly IArea serviceArea = ServiceFactory.GetService<IArea>();
        protected static readonly IMessage serviceMessage = ServiceFactory.GetService<IMessage>();
        protected static readonly IChangeGradeRecorde serviceUpGrade = ServiceFactory.GetService<IChangeGradeRecorde>();
        protected static readonly ILecturerInfo serviceLecturer = ServiceFactory.GetService<ILecturerInfo>();
        protected static readonly IAdvertisement serviceAdv = ServiceFactory.GetService<IAdvertisement>();
        protected static readonly int pageSize = Helper.PageSize;
        protected string sort = "";
        protected string order = "";
        protected int pageNumber = 1;

        protected const string VIEWLIST = "~/Views/Admin/{0}/index.cshtml";
        protected const string VIEWCREATE = "~/Views/Admin/{0}/create.cshtml";
        /// <summary>
        /// 列表视图
        /// </summary>
        protected string viewList = string.Empty;
        /// <summary>
        /// 创建或修改视图
        /// </summary>
        protected string viewCreate = string.Empty;

        protected string ajaxStr = "\"statusCode\":\"{0}\", \"message\":\"{1}\", \"navTabId\":\"{2}\", \"forwardUrl\":\"{3}\", \"callbackType\":\"{4}\"";
        private UniversallySearchParam _SearchParma = new UniversallySearchParam() { Pagination = new PaginationDTO() { CurrentIndex = 1, PageSize = pageSize } };
        protected UniversallySearchParam SearchParma
        {
            get { return _SearchParma; }
            set { _SearchParma = value; }
        }

        /// <summary>
        /// navTab ID
        /// </summary>
        protected string navTabId = string.Empty;

        private CurrentUserDTO _CurentUser = null;
        public CurrentUserDTO CurentUser
        {
            get
            {
                return _CurentUser;
            }
            set { _CurentUser = value; }
        }

        [ValidateInput(false)]
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HelperCommon common = new HelperCommon();
            ViewData["EveryDayWisdom"] = serviceEveryDayWisdom.GetByRadom();
            
            ViewData["pageSize"] = pageSize;
            ViewData["PhotoWidth"] = Helper.PhotoWidth;
            ViewData["PhotoHeight"] = Helper.PhotoHeight;
            ViewData["SearchParma"] = SearchParma;
            #region 设置当前页面
            int currentIndex = 1;
            if (Request["pageNum"] != null)
            {
                int.TryParse(Request["pageNum"], out currentIndex);
            }
            SearchParma.Pagination.CurrentIndex = currentIndex;
            #endregion

            #region 设置共用搜索参数 标题  开始结束时间
            SearchParma.Ttile = Request["Title"];
            DateTime startime = DateTime.MinValue;
            DateTime endtime = DateTime.MinValue;
            if (Request["StarTime"] != null)
            {
                if (DateTime.TryParse(Request["StarTime"], out startime))
                {
                    { SearchParma.StarDateTime = startime; }
                }
            }
            if (Request["EndTime"] != null)
            {
                if (DateTime.TryParse(Request["EndTime"], out endtime))
                { SearchParma.EndDateTime = endtime; }
            }
            #endregion

            #region 设置是否需要重新计算总记录
            if (Request["RowCount"] != null)
            {
                int rowCount = 0;
                int.TryParse(Request["RowCount"], out rowCount);
                SearchParma.Pagination.RowCount = rowCount;
            }
            #endregion
            ViewData["CurrentController"] = navTabId = filterContext.Controller.GetType().Name.Replace("Controller", "");

            #region 初始化视图路径
            viewList = string.Format(VIEWLIST, navTabId);
            viewCreate = string.Format(VIEWCREATE, navTabId);
            #endregion

            #region 获取右边栏位广告位
            ViewData["Advertisement"]= serviceAdv.GetAll(new UniversallySearchParam() { IntObj1=-1});
            #endregion
        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            ViewData["SearchParma"] = SearchParma;
        }
        protected void ExceptionHand(Exception e)
        {
            new Loger().ExceptionHand(e);
        }

        protected override void OnException(ExceptionContext errorContext)
        {
            errorContext.Result = new RedirectResult(Url.Action("Error", "Shared"));
        }

        protected void IninFlashXml(int id)
        {
            var module = serviceModule.GetByID(id);
            if (module != null)
            {
                string xmlpath ="/xml/"+ module.AdXml;
                if (System.IO.File.Exists(Server.MapPath(xmlpath)))
                {
                    TempData["xmlpath"] = xmlpath;
                }
                TempData["ModuleDetail"] = module.Remark;//模块描述
            }
            
        }
    }
}
